﻿using System;
using System.Collections.Generic;
using System.Data;
using BO.Infrastructure.Utils;
using ChannelVN.Cached;
using Utilities.Entities;

namespace BO.Video
{
    public class VideoBO
    {

        static readonly string UniqueText = typeof(VideoBO).Namespace + typeof(VideoBO).Name;

        static string BuildCacheKey(string methodName, IEnumerable<object> listParams)
        {
            return PresentationUtils.GetCacheKey(methodName, listParams, UniqueText);
        }

        public static DataTable GetNewestVideo(int top, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetNewestVideo", new object[] { top, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetNewestVideo(top, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static CategoryObject GetVideoZoneDetail(int zoneId)
        {
            var cacheKey = BuildCacheKey("GetVideoZoneDetail", new object[] { zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<CategoryObject>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetVideoZoneDetail(zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static List<CategoryObject> GetAllVideoZones()
        {
            var cacheKey = BuildCacheKey("GetAllVideoZones", null);
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<List<CategoryObject>>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetAllVideoZones();

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static DataTable GetVideoByMode(int top, int mode, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetVideoByMode", new object[] { top, mode , zoneId});
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached,cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetVideoByMode(top, mode, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static DataTable GetListVideo(int pageIndex, int pageSize, int topExclusion = 0, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetListVideo", new object[] { pageIndex, pageSize, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetListVideo(pageIndex, pageSize, topExclusion, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3)); 
                }
            }
            return result;
        }



        public static DataTable GetMostViewVideo(int pageIndex, int pageSize, int topExclusion = 0, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetMostViewVideo", new object[] { pageIndex, pageSize, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetMostViewVideo(pageIndex, pageSize, topExclusion, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static int CountVideoByZone(int videoZoneId, int topExclusion = 0)
        {
            var cacheKey = BuildCacheKey("CountVideoByZone", new object[] { videoZoneId, topExclusion});
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<int>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == default (int))
            {
                result = VideoBO_NoCache.CountVideoByZone(videoZoneId, topExclusion);

                if (result != default(int))
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static DataTable GetVideoDetail(int videoId)
        {
            var cacheKey = BuildCacheKey("GetVideoDetail", new object[] { videoId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetVideoDetail(videoId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public static DataTable GetEmbedVideo(int top, int type, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetEmbedVideo", new object[] { top, type, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = VideoBO_NoCache.GetEmbedVideo(top, type, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }
    }
}
